package me.yobol.lintcode.easy.rotatestring;

/**
 * 
 * @author Yobol
 */
public class BubbleRotateString implements IRotateString{
    
    /**
     *
     * @param str
     * @param offset
     * @return
     */
    @Override
    public char[] rotateString(char[] str,int offset){
        if (str == null || str.length == 0) {    
            return str;
        }
        char[] res = new char[str.length];
        System.arraycopy(str, 0, res, 0, str.length);
        offset %= str.length;//防止offset很长，导致重复工作
        char temp;
        while (offset != 0) {
            temp = res[res.length - 1];
            for (int i = res.length - 2; i >= 0; i--) {
                res[i + 1] = res[i];
            }
            res[0] = temp;
            offset--;
        }
        return res;
    }
}
